package com.example.web.demos.domain.dto;

import com.example.web.demos.utils.DateUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author scout.du
 * @date 2024/09/06 15:49
 **/
@NoArgsConstructor
@AllArgsConstructor
@Data
public class PrometheusResponse {


    /**
     * status : success
     * data : {"resultType":"vector","result":[{"metric":{"date":"2024-09-02"},"value":[1.725608380388E9,"5"]},{"metric":{"date":"2024-09-03"},"value":[1.725608380388E9,"5"]},{"metric":{"date":"2024-09-04"},"value":[1.725608380388E9,"5"]},{"metric":{"date":"2024-09-05"},"value":[1.725608380388E9,"11"]},{"metric":{"date":"2024-09-06"},"value":[1.725608380388E9,"2"]}]}
     */

    private String status;
    private DataBean data;



    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class DataBean {
        /**
         * resultType : vector
         * result : [{"metric":{"date":"2024-09-02"},"value":[1.725608380388E9,"5"]},{"metric":{"date":"2024-09-03"},"value":[1.725608380388E9,"5"]},{"metric":{"date":"2024-09-04"},"value":[1.725608380388E9,"5"]},{"metric":{"date":"2024-09-05"},"value":[1.725608380388E9,"11"]},{"metric":{"date":"2024-09-06"},"value":[1.725608380388E9,"2"]}]
         */

        private String resultType;
        private List<Result> result;



        @JsonInclude(JsonInclude.Include.NON_NULL)
        @NoArgsConstructor
        @AllArgsConstructor
        @Data
        public static class Result {
            /**
             * metric : {"date":"2024-09-02"}
             * value : [1.725608380388E9,"5"]
             */

            private LinkedHashMap<String,String> metric;

            private List<String> value;
            private List<List<String>> values;

            public Result convert(){

                Result result = new Result();
                result.setMetric(this.metric);
                if (!ObjectUtils.isEmpty(value)){
                    result.setValue(convertTimeStr(value));
                }
                if (!ObjectUtils.isEmpty(values)){
                    List<List<String>> collect = values.stream()
                            .filter(list -> !ObjectUtils.isEmpty(list))
                            .map(this::convertTimeStr)
                            .collect(Collectors.toList());
                    result.setValues(collect);
                }
                return result;
            }

            public List<String> convertTimeStr(List<String> source){
                String time = source.get(0);
                int index = time.indexOf(".");
                String timeStr;
                if (index>0){
                    String replace = time.replace(".", "");
                    timeStr = DateUtils.format(Long.valueOf(replace), DateUtils.STANDARD_MILL);
                } else {
                    String replace = time + "000";
                    timeStr = DateUtils.format(Long.valueOf(replace), DateUtils.STANDARD_MILL);
                }
                List<String> list = new ArrayList<>();
                list.add(timeStr);
                list.add(source.get(1));
                return list;
            }

        }
    }

    public static void convert(PrometheusResponse response){

        List<DataBean.Result> result = response.getData().getResult();
        List<DataBean.Result> list = result.stream().map(DataBean.Result::convert).toList();
        response.getData().setResult(list);
    }
}
